package com.item.mofan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.item.mofan.common.BaseContext;
import com.item.mofan.entity.Category;
import com.item.mofan.entity.Dish;
import com.item.mofan.entity.ShoppingCart;
import com.item.mofan.mapper.ShoppingCartMapper;
import com.item.mofan.service.DishService;
import com.item.mofan.service.SetmealService;
import com.item.mofan.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private ShoppingCartService shoppingCartService;
    @Override
    public ShoppingCart add(ShoppingCart shoppingCart) {
        //获取用户id
        Long userId=BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        //获取分类id
        Long dishId=shoppingCart.getDishId();
        Long setmealId=shoppingCart.getSetmealId();

        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userId);
        //根据分类获取属性
        if (dishId!=null){
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else{
            queryWrapper.eq(ShoppingCart::getSetmealId,setmealId);
        }
        ShoppingCart cartOne = shoppingCartService.getOne(queryWrapper);//查询是否在购物车中

        if (cartOne!=null){
            cartOne.setNumber(cartOne.getNumber().intValue()+1);
            shoppingCartService.updateById(cartOne);
        }else{
            shoppingCart.setNumber(1);
            shoppingCartService.save(shoppingCart);
            cartOne=shoppingCart;
        }
        return cartOne;
    }
    public List<ShoppingCart> getList(){
        Long userId=BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userId);

        List<ShoppingCart> shoppingCarts = shoppingCartService.list(queryWrapper);

        return  shoppingCarts;
   }
    public void deleteCart(ShoppingCart shoppingCart){
        //根据分类的不同，获取套餐或者菜品的id
        Long dishId=shoppingCart.getDishId();
        Long setmealId=shoppingCart.getSetmealId();
        //获取用户id
        Long userId=BaseContext.getCurrentId();
        //查询该用户下的购物车数据
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userId);

        if (dishId!=null){
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else{
            queryWrapper.eq(ShoppingCart::getSetmealId,setmealId);
        }
        //获取要删除的购物车选项
        ShoppingCart cartOne = shoppingCartService.getOne(queryWrapper);
        //判断数量是否为1，如果是则删除，如果不是则数量-1
        int num=cartOne.getNumber();
        if (num==1){
            shoppingCartService.remove(queryWrapper);
        }else{
            cartOne.setNumber(num-1);
            shoppingCartService.updateById(cartOne);
        }
    }

    /**
     * 清空购物车
     */
    @Override
    public void cleanCart() {
        //获取用户id
        Long userId=BaseContext.getCurrentId();
        //查询该用户下的购物车数据
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userId);
        shoppingCartService.remove(queryWrapper);
    }
}
